package com.stone.persistent.recyclerview.adapter

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.timeemail.R
import com.example.timeemail.letter.EmailActivity
import com.stone.persistent.recyclerview.utils.NetManager
import kotlinx.android.synthetic.main.item_feeds_product.view.*


/**
 * 商品流列表Adapter
 */
class FeedsListAdapter(context: FragmentActivity) :
    RecyclerView.Adapter<FeedsListAdapter.GoodsViewHolder>() {


    private val inflater = LayoutInflater.from(context)

    private val dataList = NetManager.getProductList()

    private val mContext = context

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GoodsViewHolder {
        val itemView = inflater.inflate(R.layout.item_feeds_product, parent, false)
        return GoodsViewHolder(itemView = itemView)
    }

    override fun getItemCount(): Int = dataList.size

    override fun onBindViewHolder(holder: GoodsViewHolder, position: Int) {
        holder.bindPosition(position, mContext)
    }

    inner class GoodsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),View.OnClickListener {

        /**
         * 刷新View
         */
        fun bindPosition(position: Int, context:Context) {
            val itemData = dataList.get(position % 6)
            itemView.goods_title_tv.text = itemData.goodName
            itemView.goods_imageview.setImageResource(itemData.imagRes)
            itemView.goods_price_tv.text = itemData.price

            val imageLp = itemView.goods_imageview.layoutParams as ConstraintLayout.LayoutParams
            imageLp.dimensionRatio = itemData.dimensionRatio.toString()
            itemView.goods_imageview.layoutParams = imageLp
            itemView.goods_imageview.setOnClickListener(this)
        }

        override fun onClick(p0: View?) {
            Log.d("消息体：", "你点击了改图片")
            val intent = Intent(mContext, EmailActivity::class.java)
            if (p0 != null) {
                p0.context.startActivity(intent)
            }
        }

    }


}

